-- 作者:文艺
-- 使用妒猫GG
-- 使用方法:
ps = [==[
悬浮球点击 - 展开/关闭功能页面菜单
悬浮球长按 - 关闭整个悬浮窗
startUi(table) table -> {
	--setImageResource = 0x7F02002C, //可选，作用是设置悬浮窗图片
	[1] = {
		text = '功能文本',
		--textColor = 0xcfffffff, //可选，作用是设置该功能文本颜色
		open = function(self) --开启
			gg.toast('功能开启')
			gg.alert(self.text .. '已开启')
		end,
		close = function(self) --关闭
			gg.toast('功能关闭')
			gg.alert(self.text .. '已关闭')
	end
	}
}
]==]
print(ps)
require(true)
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.content.*"
import "java.util.*"
import "java.lang.*"
import "android.*"
import "android.graphics.drawable.*"
import "android.graphics.PixelFormat"
import "android.ext.*"

import "loadlayout"

local context = activity
local window = context.getSystemService("window") -- 获取窗口管理器
local isswitch
local function getLayoutParams()
	local LayoutParams = WindowManager.LayoutParams
	local layoutParams = luajava.new(LayoutParams)
	if (Build.VERSION.SDK_INT >= 26) then -- 设置悬浮窗方式
		layoutParams.type = LayoutParams.TYPE_APPLICATION_OVERLAY
		else
		layoutParams.type = LayoutParams.TYPE_PHONE
	end

	layoutParams.format = PixelFormat.RGBA_8888 -- 设置背景
	layoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE -- 焦点设置Finish
	layoutParams.gravity = Gravity.CENTER -- 重力设置
	layoutParams.width = LayoutParams.WRAP_CONTENT -- 布局宽度
	layoutParams.height = LayoutParams.WRAP_CONTENT -- 布局高度
	return layoutParams
end

local function getShepeBackground(color,radiu)
	drawable = luajava.new(GradientDrawable)
	drawable.setShape(GradientDrawable.RECTANGLE)
	drawable.setColor(color)
	drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu})
	return drawable
end

function startUi(...)
	if isswitch then
		return false
	end
	isswitch=true
	local floatWindow = {
		LinearLayout;
		id = "motion";
		layout_width = "wrap_content";
		layout_height = "fill";
		orientation = "horizontal";
		{
			LinearLayout;
			layout_width = "48dp";
			layout_height = "100dp";
			orientation = "vertical";
			{
				ImageView;
				id = "control";
				layout_width = "48dp";
				layout_height = "48dp";
			};
		};
		{
			LinearLayout;
			id = "layout";
			visibility = "gone";
			layout_width = "200dp";
			layout_height = "200dp";
			orientation = "vertical";
			layout_marginLeft = "10dp";
			layout_marginRight = "10dp";
			background = getShepeBackground(0xCFF1F1F1, 20);
			{
				ScrollView;
				padding = "5dp";
				layout_width = "match_parent";
				layout_height = "match_parent";
				{
					LinearLayout;
					id = "FuncLayout";
					layout_width = "match_parent";
					orientation = "vertical";
					layout_height = "match_parent";
					background = getShepeBackground(0xC3939998, 10);
				};
			};
		};
	};

	local function invoke(arr)
		local mainLayout
		local mainLayoutParams
		local ok
		local RawX, RawY, x, y
		if type(arr) ~= 'table' then
			return error('参数需是table类型')
		end
		mainLayoutParams = getLayoutParams()
		floatWindow = loadlayout(floatWindow)
		function invoke2()
			Looper.prepare()
			if arr.setImageResource then
				control.setImageResource(arr.setImageResource)
				else
				control.setImageResource(0x7F02002C)
			end
			for i = 1, #arr do
				local value = arr[i]
				if not value.text then
					value.text = string.format('功能%d', i)
				end
				if not value.textColor then
					value.textColor = 0xFF000000
				end
				local fun = {
					Switch;
					text = value.text;
					textColor = value.textColor;
					padding = "5dp";
					layout_width = "match_parent";
					layout_height = "match_parent";
				};
				fun = loadlayout(fun)
				fun.onClick =function()
					local mode = fun.checked and 'open' or 'close'
					local func = value[mode]
					if not type(func) == 'function' then
						return false
					end
					runnable = {
						run = function()
							pcall(func,value)
						end,
					}
					ThreadManager.runOnMainThread(runnable)
				end
				FuncLayout.addView(fun)
			end
			
			window.addView(floatWindow, mainLayoutParams)
			Looper.loop()
		end
		control.onClick = function()
			layout.setVisibility(layout.getVisibility() == View.VISIBLE and View.GONE or View.VISIBLE)
		end

		local isMove

		control.onLongClick = function(v)
			if isMove then
				return false
			end
			gg.toast("悬浮窗已退出")
			window.removeView(floatWindow)
			handler.interrupt()
			isswitch = nil
		end

		control.onTouch = function(v, event)
			local Action = event.getAction()

			if Action == MotionEvent.ACTION_DOWN then
				isMove = false
				-- 点击时的XY坐标
				RawX = event.getRawX() -- 获取X坐标
				RawY = event.getRawY() -- 获取Y坐标
				x = mainLayoutParams.x -- 申明变量xmX为原X坐标
				y = mainLayoutParams.y -- 申明变量xmY为原Y坐标
			elseif Action == MotionEvent.ACTION_MOVE then
				isMove = true
				-- 移动时的XY坐标
				mainLayoutParams.x = tonumber(x) + (event.getRawX() - RawX)
				mainLayoutParams.y = tonumber(y) + (event.getRawY() - RawY)
				window.updateViewLayout(floatWindow, mainLayoutParams)
			end
		end
		handler = Handler(Looper.getMainLooper())
		runnable = {
			run = invoke2(),
		}
		handler.post(runnable)
	end

	return invoke(...)
end

startUi({
	setImageBitmap = "http://inews.gtimg.com/newsapp_bt/0/13967128652/1000.jpg",
	[1] = {
		text = '飞天',
		open = function(self)
			gg.toast('打开')
			gg.clearResults()
			gg.searchNumber(100)
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[2] = {
		text = '遁地',
		open = function(self)
			print(self)
			gg.toast('打开')
			gg.alert(self.self .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[3] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[4] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[5] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[6] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[7] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[8] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	},
	[9] = {
		open = function(self)
			gg.toast('打开')
			gg.alert(self.text .. '已打开')
		end,
		close = function(self)
			gg.toast('关闭')
			gg.alert(self.text .. '已关闭')
		end
	}
})